package com.xqls.thread;

import com.xqls.util.ObjectUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Skeyedu on 2017/4/22.
 */
public class ThreadPoolExecutorUtil {
    private static Logger logger = Logger.getLogger(ThreadPoolExecutorUtil.class);

    //获取执行线程池
    public static ExecutorService getExcutor(int size){

        return  Executors.newFixedThreadPool(size);
//        if(size>4){
//            size=size-3;
//        }
//        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(size);
//        return new ThreadPoolExecutor(3, 3, 2, TimeUnit.MINUTES, queue, new ThreadPoolExecutor.CallerRunsPolicy());
    }

    public static  void  executeCall(Callable<Void> callable){
        ExecutorService executor= ThreadPoolExecutorUtil.getExcutor(1);//执行线程
        ArrayList<Callable<Void>> listCall = new ArrayList<Callable<Void>>();
        listCall.add(callable);
        try {
            executor.invokeAll(listCall);
        } catch (Throwable e) {
            logger.error("线程异常",e);
        } finally {
            executor.shutdown();
        }

    }
    public static  void  executeCall(List<Callable<Void>> listCall){
        ExecutorService executor= ThreadPoolExecutorUtil.getExcutor(listCall.size());//执行线程
        try {
            executor.invokeAll(listCall);
        } catch (Throwable e) {
            logger.error("线程异常",e);
        } finally {
            executor.shutdown();
        }

    }

}
